package cn.mrcode.fd.blog.services.impl;

import cn.mrcode.fd.blog.dao.mapper.BlogConfigMapper;
import cn.mrcode.fd.blog.dao.mappercu.BlogConfigCuMapper;
import cn.mrcode.fd.blog.entity.BlogConfig;
import cn.mrcode.fd.blog.entityCu.BlogConfigCu;
import cn.mrcode.fd.blog.services.IBlogConfigService;
import cn.mrcode.fd.utils.MyBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author : zhuqiang
 * @version : V1.0
 * @date : 2016/1/18 21:30
 */
@Service
public class BlogConfigServiceImpl implements IBlogConfigService {
    @Autowired
    private BlogConfigMapper blogConfigMapper;
    @Autowired
    private BlogConfigCuMapper blogConfigCuMapper;

    @Override
    public BlogConfigCu findBlogConfigCu(String userId) throws Exception {
        BlogConfig selective = new BlogConfig();
        selective.setUserId(userId);
        List<BlogConfig> blogConfigs = blogConfigCuMapper.selectSelective(selective);
        BlogConfig blogConfig = blogConfigs.get(0);

        BlogConfigCu blogConfigCu = new BlogConfigCu();
        MyBeanUtils.copyProperties(blogConfigCu,blogConfig);
        return blogConfigCu;
    }
    public BlogConfig findBlogConfig(String id){
        return blogConfigMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void updateByPrimaryKeySelective(BlogConfig blogConfig) {
        blogConfigMapper.updateByPrimaryKeySelective(blogConfig);
    }

    @Override
    public Long getVisitsNum(String id) {
        return blogConfigCuMapper.selectVisitsNumById(id);
    }

    public void updateAddArticleNum(String userId) throws Exception {
        BlogConfigCu blogConfigCu = findBlogConfigCu(userId);
        Integer articleNum = blogConfigCu.getArticleNum();
        blogConfigCu.setArticleNum(articleNum+1);
        updateByPrimaryKeySelective(blogConfigCu);
    }
    @Override
    @Transactional
    public void delArticleNum(String userId, int num) throws Exception {
        BlogConfigCu blogConfigCu = findBlogConfigCu(userId);
        Integer articleNum = blogConfigCu.getArticleNum();
        blogConfigCu.setArticleNum(articleNum-num);
        updateByPrimaryKeySelective(blogConfigCu);

    }
    @Override
    public List<BlogConfig> findAll() {
        List<BlogConfig> result = blogConfigCuMapper.selectSelective(null);
        return result;
    }
    @Override
    @Transactional
    public void updateBatchResetTodayVisitsNum(){
        blogConfigCuMapper.batchResetTodayVisitsNum();
    }


}
